<?php
class BookmakersController extends AppController {
    // var $helpers = array ('Html','Form','Session');
    //var $modelClass = 'Multa';
	var $components = array ('Upload', 'Thumb');
    var $name = 'Bookmaker'; // riferimento alla cartella delle view
            
    public function beforeFilter(){
         
		parent::beforeFilter();
		// tutte le azioni di questo controller possono essere eseguite
		 $this->Auth->allow("index","view","links","detail");
    }
    
    public function index(){
        $this->layout="home";
        $this->Bookmaker->hasMany["BookmakerLang"]["conditions"]=array("BookmakerLang.locale" => Configure::read("Config.language"));
        $bookmaker_list = $this->Bookmaker->find("all",array("conditions"=>array("deleted"=>0), "order" => array("Bookmaker.valutazione desc")));
		$this->set("title_for_layout",  __("I migliori bookmakers"));
		$this->set("meta_description",  __("meta_description"));
        $this->set("bookmakers",$bookmaker_list);
            
    }
    
    public function view($id){
       $this->Bookmaker->recursive = 3;
   	   $this->Bookmaker->Links->unbindModel(array('belongsTo' => array('Lang')));
   	   // $this->Bookmaker->Pagamento->unbindModel(array('hasMany' => array('PagamentoLang')));
   	   $this->Bookmaker->Pagamento->unbindModel(array('belongsTo' => array('Bookmaker')));
   	   $this->Bookmaker->Logo->unbindModel(array('belongsTo' => array('AttachTipo')));
   	   $this->Bookmaker->LogoSlim->unbindModel(array('belongsTo' => array('AttachTipo')));
   	   $this->Bookmaker->Pagamento->Metodo->hasMany["CategoryLang"]["conditions"]=array("CategoryLang.locale" => Configure::read("Config.language"));
   	   $this->Bookmaker->Pagamento->hasMany["PagamentoLang"]["conditions"]=array("PagamentoLang.locale" => Configure::read("Config.language"));
       $this->Bookmaker->hasMany["BookmakerLang"]["conditions"]=array("BookmakerLang.locale" => Configure::read("Config.language"));
       $bookmaker  = $this->Bookmaker->read(null,$id);
	   // pr($bookmaker);die;
	   $this->set("breadcrumb",__("Dettagli Bookmaker"));
       $this->set("bookmaker",$bookmaker);
	   $this->set("title_for_layout", "Bookmaker " . $bookmaker["Bookmaker"]["nome"]);
	   $this->set("meta_description",  __("Su %s trovi i bonus di benvenuto e promozioni sullo sport, registrati!",$bookmaker["Bookmaker"]["nome"]));
    }
	 
    public function detail($name){
        $bookmaker = $this->Bookmaker->find("first",array("conditions"=>array("deleted"=>0,"nome_slug"=>$name)));
		// pr($bookmaker);die;
		$this->view($bookmaker["Bookmaker"]["id"]);
		$this->render("/Bookmaker/view");
            
    }
	 
    public function links($id){
        $this->Bookmaker->hasMany["BookmakerLang"]["conditions"]=array("BookmakerLang.locale" => Configure::read("Config.language"));
        $bookmaker  = $this->Bookmaker->read(null,$id);
		$this->set("breadcrumb",__("Link Bookmaker"));
        $this->set("bookmaker",$bookmaker);
    }
    


	
	public function update_logo_slim() {
        $this->layout="ajax";
        $this->autorender = false;
    	$ds = null;
		$data=array();
        $filesuploaded = array();
	    try {
		    $ds = $this->Bookmaker->getDataSource();
			$ds->begin();
			// salvo i file su attach se mi vengono passati dal form
			$filesuploaded = $this->Upload->execute();
			foreach ($filesuploaded as $fileuploaded) {
				// pr($this->Jobber->id);
	            $this->Bookmaker->read(null,$this->request->data["objectId"]);
	            $this->Bookmaker->set('logo_slim',$fileuploaded['id']);
	            $this->Bookmaker->save();
			}
			// --------------------------------------------------------
			$ds->commit();
			$data["success"] = true;
            $data["message"] = __("Numero file salvati: ") . count($filesuploaded);
            $data["filesuploaded"] = $filesuploaded;
			echo json_encode($data);
            die();
	    } catch (Exception $e) {
	    	if (isset($ds)) {
	    		$ds->rollback();
	    	}
	    	CakeLog::error($e->getMessage());
			$data = array('success'=> false,'message'=>$e->getMessage(),'level'=>'error','filesuploaded' => $filesuploaded);
			echo json_encode($data);
            die();
	    }
	}

	public function update_logo() {
    	//fatto da DARIO LICCI :inizio    
        $this->layout="ajax";
        $this->autorender = false;
    	$ds = null;
		$data=array();
        $filesuploaded = array();
	    try {
		    $ds = $this->Bookmaker->getDataSource();
			$ds->begin();
			// salvo i file su attach se mi vengono passati dal form
			$filesuploaded = $this->Upload->execute();
			foreach ($filesuploaded as $fileuploaded) {
				// pr($this->Jobber->id);
	            $this->Bookmaker->read(null,$this->request->data["objectId"]);
	            $this->Bookmaker->set('logo',$fileuploaded['id']);
	            $this->Bookmaker->save();
			}
			// --------------------------------------------------------
			$ds->commit();
			$data["success"] = true;
            $data["message"] = __("Numero file salvati: ") . count($filesuploaded);
            $data["filesuploaded"] = $filesuploaded;
			echo json_encode($data);
            die();
	    } catch (Exception $e) {
	    	if (isset($ds)) {
	    		$ds->rollback();
	    	}
	    	CakeLog::error($e->getMessage());
			$data = array('success'=> false,'message'=>$e->getMessage(),'level'=>'error','filesuploaded' => $filesuploaded);
			echo json_encode($data);
            die();
	    }
	}
    
    public function edit($id){
	   $this->layout="admin";
       $this->Bookmaker->recursive = 3;
   	   $this->Bookmaker->Links->unbindModel(array('belongsTo' => array('Lang')));
   	   $this->Bookmaker->Pagamento->unbindModel(array('hasMany' => array('PagamentoLang')));
   	   $this->Bookmaker->Pagamento->unbindModel(array('belongsTo' => array('Bookmaker')));
   	   $this->Bookmaker->Logo->unbindModel(array('belongsTo' => array('AttachTipo')));
   	   $this->Bookmaker->LogoSlim->unbindModel(array('belongsTo' => array('AttachTipo')));
   	   $this->Bookmaker->Pagamento->Metodo->hasMany["CategoryLang"]["conditions"]=array("CategoryLang.locale" => Configure::read("Config.language"));
       $bm = $this->Bookmaker->read(null,$id);
       $this->set("bookmaker",$bm); 
	   // pr($bm);die;
	   $this->set("breadcrumb",__("Modifica Bookmaker"));
       $this->set("valute",$this->Session->read("valutalist")); 
       $this->set("categorie",$this->Session->read("categorylist")); 
       $this->set("pagamenti",$this->Session->read("pagamentolist")); 
        
    }
    
    public function add(){
	   $this->layout="admin";
	   $this->set("breadcrumb",__("Aggiungi Bookmaker"));
       $this->set("valute",$this->Session->read("valutalist")); 
       $this->set("categorie",$this->Session->read("categorylist")); 
        //$bm = $this->Bookmaker->create();
        //$this->set("bookmaker",$bm); 
    }
    
    public function save($id=null){
        isset($this->request->data["Bookmaker"]["valuta"]) && $this->request->data["Bookmaker"]["valuta"] = implode(",",$this->request->data["Bookmaker"]["valuta"] );
        isset($this->request->data["Bookmaker"]["pagamento"]) && $this->request->data["Bookmaker"]["pagamento"] = implode(",",$this->request->data["Bookmaker"]["pagamento"] );
        isset($this->request->data["Bookmaker"]["categoria"]) && $this->request->data["Bookmaker"]["categoria"] = implode(",",$this->request->data["Bookmaker"]["categoria"] );
        !isset($this->request->data["Bookmaker"]["valuta"]) && $this->request->data["Bookmaker"]["valuta"] = "";
        !isset($this->request->data["Bookmaker"]["pagamento"]) && $this->request->data["Bookmaker"]["pagamento"] = "";
        !isset($this->request->data["Bookmaker"]["categoria"]) && $this->request->data["Bookmaker"]["categoria"] = "";
        
        $this->Bookmaker->set($this->request->data);
        if($this->Bookmaker->save()){
            $this->Session->setFlash("Bookmaker aggiornato con successo");
            $this->redirect("/Bookmakers/edit/".$this->Bookmaker->id);
        } else {
            $this->redirect($this->referer());
        }
        
        
    }
    
    public function delete($value)
    {
        //$this->Bookmaker->unbindModel(array('hasMany' => array('BookmakerLang')));
        $this->Bookmaker->read(null,$value);
        $this->Bookmaker->set("deleted",1);
        $this->Bookmaker->save();  
              
        $this->Session->setFlash("Bookmaker eliminato con successo");
        $this->redirect("/Bookmakers/index");
    }

}

?>